<template>
  <view class="page container">
    <u-form
      :model="basicForm"
      :rules="rules"
      labelPosition="top"
      ref="form"
      labelWidth="auto"
      :labelStyle="{
				fontSize: '14px',
				color: '#B2B2B2',
				lineHight: '20px'
      }"
    >
			<view class="" style="max-height: 70vh; overflow-y: auto;">
				<u-form-item label="采购大类" prop="provisionBroad" required>
					<h-dropdown
						class="w-100"
						keyName="categoryName"
						placeholder="请选择"
						v-model="basicForm.provisionBroad"
						:api="api.erpPurchaseCategoryList"
						dataKey="result"
						suffixIcon="arrow-down-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }"
						@change="provisionBroadChange"
					/>
				</u-form-item>
				<u-form-item label="采购小类" prop="provisionSmall" required>
					<h-dropdown
						ref="provisionSmall"
						class="w-100"
						keyName="categoryName"
						placeholder="请选择"
						v-model="basicForm.provisionSmall"
						:params="{ parentId: basicForm.provisionBroad }"
						:api="api.erpPurchaseCategoryChildList"
						dataKey="result"
						suffixIcon="arrow-down-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }"
						@change="provisionSmallChange"
					/>
				</u-form-item>
				<u-form-item label="物料名称" prop="materialName" required>
					<h-select-material-input-popup
						style="width: 100%"
						v-model="basicForm.materialName"
						placeholder="请选择物料名称"
						border="surround"
						:materialFlag="true"
						auditStatus="2"
						:provisionSmall="basicForm.provisionSmall"
						@change="materialNameBack"
						suffixIcon="arrow-down-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }"
					></h-select-material-input-popup>
				</u-form-item>
				<u-form-item label="物料编号" prop="materialBillId" required>
					<u--input placeholder="请输入物料编号" disabled v-model="basicForm.materialBillId" />
				</u-form-item>
				<u-form-item label="品牌" prop="brand">
					<u--input placeholder="请输入品牌" disabled v-model="basicForm.brand" />
				</u-form-item>
				<u-form-item label="规格型号" prop="specification">
					<u--input placeholder="请输入规格型号" disabled v-model="basicForm.specification" />
				</u-form-item>
				<u-form-item label="单位" prop="unit">
					<h-select-input-tag
						disabled
						class="w-100"
						v-model="basicForm.unit"
						objectField="model.unit"
						placeholder="请选择单位"
						border="surround"
						suffixIcon="arrow-down-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }"
						dictName="unit_type"
					></h-select-input-tag>
				</u-form-item>
				<u-form-item label="数量" prop="amount" required>
					<h-number-input placeholder="请输入数量" v-model="basicForm.amount" :digit="0" :min="1"></h-number-input>
				</u-form-item>
				<u-form-item label="单价(元)" prop="unitPrice" required>
					<!-- <h-number-input placeholder="请输入单价(元)" v-model="basicForm.unitPrice" :digit="2" :min="1"></h-number-input> -->
					<h-money-input placeholder="请输入单价" v-model="basicForm.unitPrice"></h-money-input>
				</u-form-item>
				<u-form-item label="备注" prop="remark">
					<u--textarea :confirm-type="null" count v-model="basicForm.remark" placeholder="请输入备注" maxlength="500"></u--textarea>
				</u-form-item>
			</view>

      <view class="space-x-1 mt-1" style="display: flex">
        <u-button type="primary" text="保存" :loading="loading" @click="onSubmit"></u-button>
        <u-button type="primary" text="关闭" :loading="loading" @click="onClose"></u-button>
      </view>
    </u-form>
  </view>
</template>

<script>
import { erpPurchaseCategoryList, erpPurchaseCategoryChildList, erpPurchaseCannibalizeCheckStock } from '@/api/index'
export default {
  name: 'FormC',
  props: {
    teamId: {
			type: String,
			default: '',
			required: false
		},
    companyId: {
			type: String,
			default: '',
			required: false
		},
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
		exportWarehouse: {
			type: String,
			default: '',
			required: false
		},
		storehouseName:{
			type: String,
			default: '',
			required: false
		},
    datailDisabled: {
      type: Boolean,
      default: false,
    },
    index: {
      type: Number,
      default: null,
    },
  },
  data() {
    return {
      loading: false,
      ratioPaymentNum: 100,
      api: {
        erpPurchaseCategoryList,
        erpPurchaseCategoryChildList,
      },
      basicForm: {
        provisionBroad: '',
        provisionSmall: '',
        materialName: '',
        materialBillId: '',
        brand: '',
        specification: '',
        unit: '',
        amount: '',
        unitPrice: '',
        remark: '',
      },
      rules: {
        provisionBroad: [{ required: true, message: '请选择采购大类!', trigger: 'change' }],
        provisionSmall: [{ required: true, message: '请选择采购小类!', trigger: 'change' }],
        materialName: [{ required: true, message: '请选择物料名称!', trigger: 'change' }],
        supplier: [{ required: true, message: '请输入供应商!', trigger: 'blur' }],
        materialBillId: [{ required: true, message: '请输入物料编号!', trigger: 'blur' }],
        unitPrice: [
          { required: true, message: '请输入单价!', trigger: 'blur' },
          { required: true, pattern: /^(?!0\.00)(?!0$)([1-9]\d*|0)(\.\d{2})?$/, message: '请输入正确的金额!', trigger: ['change', 'blur'] },
        ],
        amount: [
          { type: 'number', required: true, message: '请输入数量!', trigger: 'blur' },
          { required: true, pattern: /^[0-9]*[1-9][0-9]*$/, message: '请输入正整数!', trigger: ['change', 'blur'] },
        ],
      },
			firstClick: 0
    }
  },
  created() {},
  methods: {
    provisionBroadChange() {
			// cthis.basicForm.id&& id还能为零我是不能理解的 编号是DB032023111400121
			if(this.firstClick==1){
				this.firstClick++
			} else {
				// $set 解决修改值后数据不响应的问题
				this.$set(this.basicForm, 'provisionSmall', '')
				this.provisionSmallChange()
			}
      // $nextTick 解决大类修改后小类接口 params 未更新的问题
      this.$nextTick(() => {
        this.$refs.provisionSmall.loadData()
      })
    },
		provisionSmallChange(){
			this.$set(this.basicForm, 'materialName', null)// 物料名称
			this.$set(this.basicForm, 'materialBillId', null)// 物料编码
			this.$set(this.basicForm, 'specification', null)// 规格型号
			this.$set(this.basicForm, 'unit', null)// 单位
			this.$set(this.basicForm, 'brand', null)// 品牌
			this.$set(this.basicForm, 'amount', null)//数量
			this.$set(this.basicForm, 'unitPrice', null)//单价
		},
    materialNameBack(material) {
			if (!material) {
				this.$set(this.basicForm, 'materialBillId', null) // 物料编码
				this.$set(this.basicForm, 'specification', null) // 规格型号
				this.$set(this.basicForm, 'unit', null) // 单位
				this.$set(this.basicForm, 'brand', null) // 品牌
				this.$set(this.basicForm, 'amount', null) //数量
				this.$set(this.basicForm, 'unitPrice', null) //单价
				return false
			}
      this.$set(this.basicForm, 'materialBillId', material.materialBill) // 物料编码
      this.$set(this.basicForm, 'specification', material.specification) // 规格型号
      this.$set(this.basicForm, 'unit', material.unit) // 单位
      this.$set(this.basicForm, 'brand', material.brand) // 品牌
      this.$set(this.basicForm, 'amount', null) //数量
      this.$set(this.basicForm, 'unitPrice', null) //单价
    },
    // 税率输入框变化时
    inputTaxRateBlur(value) {
      const num = this.basicForm.ratioPayment.toString()
      if (num >= 0 && num <= this.ratioPaymentNum) {
        this.basicForm.ratioPayment = num
      }
      if (num < 0) {
        this.basicForm.ratioPayment = 0
      }
      if (num > this.ratioPaymentNum) {
        this.basicForm.ratioPayment = this.ratioPaymentNum
        this.$u.toast('付款信息列表中付款总比例不能超过100%！')
      }
    },
    reset() {
      this.$refs.form.resetFields()
    },
		async getCheckStock () {
			const res = await erpPurchaseCannibalizeCheckStock({
				storehouseId: this.exportWarehouse,
				materialName: this.basicForm.materialName,
				amount: this.basicForm.amount
			})
			if (res.success && !!res.result && res.result.isOk) {
				return Promise.resolve()
			} else {
				this.$u.toast("（调出团队） "+this.storehouseName+" 仓库 "+this.basicForm.materialName_dictText+" "+res.message)
				return Promise.reject("（调出团队） "+this.storehouseName+" 仓库 "+this.basicForm.materialName_dictText+" "+res.message)
			}
		},
    async onSubmit() {
      await this.$refs.form.validate()
			if(this.loading) return
			this.loading = true
      if (!this.index) {
        this.$emit('submit', [
          {
            id: parseInt(Math.random() * 1000000000000),
            ...this.basicForm,
          },
          this.index,
        ])
      } else {
        this.$emit('submit', [this.basicForm, this.index])
      }
    },
    onClose() {
			this.loading = false
      this.$emit('close')
    },
  }
}
</script>

<style scoped>
.page.container {
	padding: 20px;
}
.basic-style {
	margin: 0;
	height: 500px;
}
.u-form-item {
	padding: 0px 8px;
}
</style>
